
// 福利卡图片功能逻辑实现
import { makePicMessage, makePicLocal } from "../utils/filesBox.js"
import delay from "../utils/delay.js";
import { getprettyPic } from "../api/index.js"
import { downloadUrl, removeFile } from "../utils/fs.js"
import { getRandomInt } from "../utils/index.js";
import request from "request"
const TypeList = ['女仆', 'jk制服', '兔女郎', '夏日泳装', '动漫类', '幼齿', '萝莉', '少女', '御姐', '巨乳', '丰满微胖', '黑丝', '白丝', '肉丝', '网丝', '吊带袜', '腿控', '脚控', '臀控', '旗袍']
const rule = `请告诉我你想要的照片类型^__^:\n\n0:随机生成 1:女pu 2:jk制服\n\n3:兔女郎 4:夏日永莊 5:动漫\n\n6:幼尺 7:罗丽 8:少女\n\n9：御姐 10：巨茹 11：丰满\n\n12:黑si 13:白si 14：肉si\n\n15：网si 16:调待袜 17:腿控\n\n18：脚控 19：豚控 20：旗袍\n\n请用“#”拼接你想要的类型，茹：福利图片#2`
const getPic = async (room, content) => {
    try {
        let key = content.split("#")[1] || content.split('＃')[1] || getRandomInt(0,20)
        if (!key) {
            room.say(rule)
            return
        }
        key = parseInt(key)
        let res = null
        let dataList = []
        let sendFile = null
        if (key > 0) {
            let sort = TypeList[key - 1]
            res = await getTypePic(sort)
            dataList = res.imgurl || []
            let num = Math.floor(Math.random() * (0 - dataList.length + 1)) + dataList.length
            sendFile = makePicMessage(dataList[num])
        } else {
            res = await getprettyPic()
            const data = await downloadUrl(res.data.url)
            console.log(data)
            sendFile = makePicLocal(data.name)
            setTimeout(() => {
            removeFile(data.name)
            }, 1000);
            //    res.data.url
            //     sendFile
        }

        console.log(sendFile)
        if (sendFile) {
            console.log('发送')
            delay(1000)
            await room.say(sendFile)
        }
        return
        //随机生成
        if (res.data?.length) {
            let fileList = res.data.map(it => {
                return makePicMessage(it.imageUrl)
            })
            console.log(fileList.length)
            fileList.map(async it => {
                delay(1000)
                await room.say(it)
            })
        }

    } catch (e) {
        console.log(e)
    }
}

const getTypePic = (key) => {
    console.log('类型 ' + key)
    return new Promise((resolve, reject) => {
        const url = `https://api.uomg.com/api/rand.img4?sort=${key || 'jk制服'}&format=json`
        var options = {
            'method': 'POST',
            'url': encodeURI(url),
            'headers': {
                'Content-Type': 'application/json'
            },
        }
        request(options, function (error, response) {
            if (error) {
            }

            resolve(JSON.parse(response.body))
        })
    })
}

export default getPic